package br.cin.stadium.middleware.media;

import br.cin.stadium.middleware.FormatException;
import br.cin.stadium.middleware.directory.ServerLocation;
import br.cin.stadium.middleware.marshall.MarshallInputStream;
import br.cin.stadium.middleware.marshall.MarshallOutputStream;

/**
 * 
 * @author Fabiano, Jorge, Rafael, Vando
 * 
 */
public class MediaReference extends ObjectSerializable
{

	private ServerLocation location;
	private String mediaName;
	private int sector;

	public MediaReference(ServerLocation location, String mediaName, int sector)
	{
		this.location = location;
		this.mediaName = mediaName;
		this.sector = sector;
	}

	public MediaReference(String port, String mediaName, int sector)
	{
		this.location = new ServerLocation("", port);
		this.mediaName = mediaName;
		this.sector = sector;
	}

	public MediaReference()
	{
		this.location = new ServerLocation();
	}

	public void setLocation(ServerLocation location)
	{
		this.location = location;
	}

	public void setMediaName(String mediaName)
	{
		this.mediaName = mediaName;
	}

	/**
	 * @return the ip
	 */
	public ServerLocation getServerLocation()
	{
		return this.location;
	}

	/**
	 * @return the mediaName
	 */
	public String getMediaName()
	{
		return mediaName;
	}

	/**
	 * 
	 * @return
	 */
	public int getSector()
	{
		return sector;
	}

	/**
	 * 
	 * @param sector
	 */
	public void setSector(int sector)
	{
		this.sector = sector;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * br.cin.stadium.middleware.media.ObjectSerializable#marshal(br.cin.stadium
	 * .middleware.marshall.MarshallOutputStream)
	 */
	public void marshal(MarshallOutputStream mos) throws FormatException
	{
		mos.writeUTF(this.getServerLocation().getIp());
		mos.writeUTF(this.getServerLocation().getPort());
		mos.writeUTF(this.getMediaName());
		mos.writeInt(this.getSector());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * br.cin.stadium.middleware.media.ObjectSerializable#unmarshal(br.cin.stadium
	 * .middleware.marshall.MarshallInputStream)
	 */
	public void unmarshal(MarshallInputStream mis) throws FormatException
	{
		this.getServerLocation().setIp(mis.readUTF());
		this.getServerLocation().setPort(mis.readUTF());
		this.setMediaName(mis.readUTF());
		this.setSector(mis.readInt());
	}
	
	public String toString() {
		String retorno = "IP = " + this.getServerLocation().getIp() + "\n" + 
		                 "PORT = " + this.getServerLocation().getPort() + "\n" + 
		                 "Name = " + this.getMediaName() + "\n" + 
		                 "Sector = " + this.getSector();
		
		return retorno;
		
	}
}